//
//  CollectionFlowLayoutVC.swift
//  Example
//
//  Created by remy on 2019/12/9.
//  Copyright © 2019 com.Risac. All rights reserved.
//

class CollectionFlowLayoutVC: BaseVC {
    private lazy var pack: CollectionPack = CollectionPack(frame: self.bodyViewFrame)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        topBarView.centerItem = .text(title)
        topBarView.rightItems = [.text("切换方向") {
            $0.margin.right = 16.0
            $0.action(UITapGestureRecognizer(target: self, action: #selector(self.reloadTap)))
        }]
        pack.view.backgroundColor = FlatColor.greenSea
        pack.layout = CollectionFlowLayout(model: pack.model)
        self.view.addSubview(pack.view)
        let sections: [CollectionFlowLayoutSectionItem] = (0..<2).map {
            let item = CollectionFlowLayoutSectionItem(index: $0)
            item.cellItems = (0..<10).map {
                let item = CollectionFlowLayoutItem(index: $0)
                if $0 % 3 == 0 {
                    item.cellSize = CGSize(width: 100.0, height: 50.0)
                }
                return item
            }
            return item
        }
        pack.model.set(sections)
        pack.view.reloadData()
    }
    
    @objc private func reloadTap() {
        if pack.direction == .vertical {
            pack.layout = CollectionFlowLayout(model: pack.model, direction: .horizontal)
        } else {
            pack.layout = CollectionFlowLayout(model: pack.model)
        }
        pack.view.contentOffset = .zero
        pack.view.reloadData()
    }
}

class CollectionFlowLayoutSectionItem: CollectionSectionItem {
    init(index: Int) {
        super.init()
        sectionStageClass = CollectionFlowLayoutStage.self
        lineSpacing = 10.0
        itemSpacing = 5.0
        sectionSpacing = 20.0
        sectionMargin = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
        sectionInset = .zero
        flowCount = 3 + index * 2
    }
}

class CollectionFlowLayoutStage: CollectionSection {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = FlatColor.amethyst
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateSection(sectionItem: CollectionSectionItem?) {}
}

class CollectionFlowLayoutItem: CollectionCellItem {
    let index: Int
    
    init(index: Int) {
        self.index = index
        super.init()
        cellClass = CollectionFlowLayoutCell.self
        cellSize = CGSize(width: CGFloat(arc4random() % 100 + 20), height: CGFloat(arc4random() % 100 + 20))
    }
}

class CollectionFlowLayoutCell: CollectionCell {
    private lazy var label: UILabel = UILabel(textColor: FlatColor.midnightBlue, fontSize: 14.0, bold: true, bgColor: FlatColor.orange, alignment: .center)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.backgroundColor = FlatColor.wetAsphalt
        contentView.addSubview(label)
        label.snp.makeConstraints {
            $0.edges.equalToSuperview().inset(UIEdgeInsets(top: 5.0, left: 5.0, bottom: 0.0, right: 0.0))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateCell(cellItem: CollectionCellItem?) {
        guard let item = cellItem as? CollectionFlowLayoutItem else { return }
        label.text = "\(item.index)"
    }
}
